
import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        long sTime = System.currentTimeMillis();
        StringBuilder b = new StringBuilder();
        loop:
        for (int i = 1000; i < 10000; i++) {
            if (isPrime(i)) {
                for (int j = 2; j < 10000; j++) {
                    if (isPrime(i + j) && isPrime(i + j + j)) {
                        if(arePermu(i, i + j, i + j + j) && i != 1487) {
                            b.append(i);
                            b.append(i + j);
                            b.append(i + j + j);
                            break loop;
                        }
                    }
                }
            }
        }
        System.out.println(b);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }

    static boolean isPrime(int i) {
        for (int j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }

    static boolean arePermu(int i, int j, int k) {
        String l = String.valueOf(i);
        StringBuilder b = new StringBuilder(l);
        String l2 = String.valueOf(j);
        StringBuilder b2 = new StringBuilder(l2);
        String l3 = String.valueOf(k);
        StringBuilder b3 = new StringBuilder(l3);
        String[] s = new String[l.length()];
        if (l.length() == l2.length() && l2.length() == l3.length()) {
            if (!arePermu(b, b2) || !arePermu(b, b3)){
                return false;
            }
        } else {
            return false;
        }
        return true;
    }

    static boolean arePermu(StringBuilder a, StringBuilder b) {
        char aIndex[] = new char[a.length()];
        char bIndex[] = new char[b.length()];
        for(int i = 0; i < a.length(); i++) {
            aIndex[i] = a.charAt(i);
            bIndex[i] = b.charAt(i);
        }
        Arrays.sort(aIndex);
        Arrays.sort(bIndex);
        int numSame = 0;
        for(int i = 0; i < aIndex.length; i++) {
            if (aIndex[i] == bIndex[i]) {
                numSame++;
            }
        }
        return (numSame == a.length());
    }
}